import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

/**
 * Part of both the expert and wizard mode. Deals with selecting files
 * for input.
 * @author James Stanier
 */
public class FileIOGUI implements ActionListener {
	JFrame window;
	JFrame prevWindow;
	String mode;

	JButton fileChButton;
	JLabel filesChosen;
	JLabel fileIntro;
	JButton backButton;
	JButton nextButton;

	boolean firstAddition;
	ArrayList<File> filesSelected;

	/**
	 * Constructor.
	 * @param w The previous window.
	 * @param s The mode we are operating in; wizard or expert.
	 */
	public FileIOGUI(JFrame w, String s) {
		mode = s;
		prevWindow = w;
		firstAddition = true;
		filesSelected = new ArrayList<File>();
		fileIOScreen();
	}

	/**
	 * Configures and draws the file IO screen.
	 */
	public void fileIOScreen() {
		window = new JFrame("JavaLiN - File input");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setSize(350,350);
		window.setLocationRelativeTo(null);
		window.toFront();
		window.setResizable(false);

		// Introductory passage and logo
		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		top.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		// Logo
		JLabel logo = new JLabel();
		logo.setIcon(new ImageIcon("logo.png"));
		logo.setHorizontalAlignment(JLabel.CENTER);
		top.add(logo, BorderLayout.NORTH);

		// Intro text
		JLabel intro = new JLabel("<HTML>Please select the Java files for input.</HTML>");
		intro.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		intro.setHorizontalAlignment(JLabel.CENTER);
		top.add(intro, BorderLayout.CENTER);

		// File choose button
		fileChButton = new JButton("Open...");
		fileChButton.addActionListener(this);
		top.add(fileChButton, BorderLayout.SOUTH);

		// List of chosen files
		JPanel fileListPanel = new JPanel();
		fileListPanel.setLayout(new BorderLayout());
		fileListPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

		fileIntro = new JLabel();
		fileListPanel.add(fileIntro, BorderLayout.NORTH);
		filesChosen = new JLabel("");
		//filesChosen.setPreferredSize(new Dimension(200,300));
		fileListPanel.add(filesChosen, BorderLayout.SOUTH);

		// Buttons
		JPanel navButtons = new JPanel();
		navButtons.setLayout(new BorderLayout());
		navButtons.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		backButton = new JButton("<< Back");
		backButton.addActionListener(this);
		navButtons.add(backButton, BorderLayout.WEST);
		nextButton = new JButton("Next >>");
		nextButton.addActionListener(this);
		nextButton.setEnabled(false);
		navButtons.add(nextButton, BorderLayout.EAST);

		window.add("North", top);
		window.add("Center", fileListPanel);
		window.add("South", navButtons);
		window.validate();
		window.setVisible(true);
	}

	/**
	 * Actions to be taken upon component interaction.
	 */
	public void actionPerformed(ActionEvent e) {
		Object x = e.getSource();
		if(x.equals(fileChButton)) {
			JFileChooser fc = new JFileChooser(".");
			fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			fc.setFileFilter(new JavaFileFilterJava());

			int returnVal = fc.showOpenDialog(fc);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				try {
					File f = fc.getSelectedFile();
					filesSelected.add(f);

					if(!firstAddition) {
						String temp = filesChosen.getText();
						temp = temp.substring(0, temp.length()-7);
						filesChosen.setText(temp + ", " + f.getName() + "</b></HTML>");
					}
					else {
						fileIntro.setText("<HTML>Files chosen:</HTML>");
						firstAddition = false;
						filesChosen.setText("<HTML><b>" + f.getName() + "</HTML>");
					}
					nextButton.setEnabled(true);
				}
				catch(Exception ex) {}
			}
		}
		else if(x.equals(backButton)) {
			window.setVisible(false);
			filesSelected = new ArrayList<File>();
			firstAddition = true;
			filesChosen.setText("");
			prevWindow.setVisible(true);
		}
		else if(x.equals(nextButton)) {

			if(filesSelected.isEmpty()) {

			}

			window.setVisible(false);

			FileOutputGUI fogui = new FileOutputGUI(window, mode, filesSelected);

		}
	}
}

/**
 * A file filter for use in the JFileChooser. Filters out files
 * that do not have the .java extension.
 * @author James Stanier
 */
class JavaFileFilterJava extends javax.swing.filechooser.FileFilter {
	public boolean accept(File f) {
		return f.getAbsolutePath().toLowerCase().endsWith(".java");
	}
	public String getDescription() {
		return "Java files";
	}
}
